﻿using System;
using System.Collections.Generic;
using SAPbouiCOM.Framework;

namespace ApexInterest
{
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application oApp = null;
            if (args.Length < 1)
            {
                oApp = new Application();
            }
            else
            {
                oApp = new Application(args[0]);
            }

            Function.Company = Application.SBO_Application.Company.GetDICompany() as SAPbobsCOM.Company;

            if (Function.Company.Connected)
            {
                Application.SBO_Application.SetStatusBarMessage("Addon started", SAPbouiCOM.BoMessageTime.bmt_Short, false);
                Application.SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler(SBO_Application_AppEvent);
                oApp.Run();
                Function.Company.Disconnect();
                System.Windows.Forms.Application.Exit();

            }
            else
            {
                Application.SBO_Application.SetStatusBarMessage("Addon failed to connect Company", SAPbouiCOM.BoMessageTime.bmt_Short, true);
                System.Windows.Forms.Application.Exit();
            }
        }

        static void SBO_Application_AppEvent(SAPbouiCOM.BoAppEventTypes EventType)
        {
            switch (EventType)
            {
                case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged:
                case SAPbouiCOM.BoAppEventTypes.aet_LanguageChanged:
                case SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition:
                case SAPbouiCOM.BoAppEventTypes.aet_ShutDown:
                    System.Windows.Forms.Application.Exit();
                    break;
            }
        }
    }
}
